// 封装ElMessage
import IconInfo from '@/components/SvgIcon/IconInfo'
import IconSuccess from '@/components/SvgIcon/IconSuccess'
import IconWarning from '@/components/SvgIcon/IconWarning'
import IconError from '@/components/SvgIcon/IconError'

let instance = null
/**
 * 消息提示
 * @param type [success,warning,error]
 * @param message 消息内容
 * @param showClose 是否显示关闭按钮
 * @param duration 自动关闭时间(毫秒)，0为不自动关闭
 */
export const Emessage = (type, message, showClose, duration) => {
  instance?.close()
  let icon = h(IconInfo)
  switch (type) {
    case 'info':
      icon = h(IconInfo)
      break
    case 'success':
      icon = IconSuccess
      break
    case 'warning':
      icon = IconWarning
      break
    case 'error':
      icon = IconError
      break
  }
  if (IsEmpty(duration)) {
    duration = 3000
  }
  instance = ElMessage({
    type: type,
    message,
    icon,
    duration,
    showClose
  })
}
/**
 * 询问确认框
 * @param message 询问内容
 * @param title 询问标题
 * @param customClass 自定义类名
 * @param confirmButtonText 确认按钮文字
 * @param cancelButtonText 取消按钮文字
 */
export const EmessageBox = (message, title, customClass, confirmButtonText, cancelButtonText) => {
  return ElMessageBox.confirm(message || '😂😂😂', title || '标题', {
    draggable: true,
    customClass,
    confirmButtonText,
    cancelButtonText,
    closeOnClickModal: false,
    dangerouslyUseHTMLString: true
  })
}

/**
 * 请求失败
 * @param res 请求返回的响应
 */
export const requestError = (res) => {
  Emessage('error', res?.data?.error || res.message)
}

